package leetcode;

/**
 * @author ht
 * @create 2025-06-18 22:45
 * @desc
 **/
public class T59 {

    public int[][] generateMatrix(int n) {
        int startX = 0; //每圈的开始X坐标
        int startY = 0; //每圈的开始Y坐标
        int count = 1;
        int[][] nums = new int[n][n];
        int loop = 1;
        int i, j = 0;

        while (loop <= n /2) {

            for (j = startY; j < n - loop; j++) {
                nums[startX][j] = count++;
            }

            for (i = startX; i < n - loop; i++) {
                nums[i][j] = count++;
            }

            for ( ; j > startY; j--) {
                nums[i][j] = count++;
            }

            for ( ; i > startX; i--) {
                nums[i][j] = count++;
            }

            loop++;
            startX++;
            startY++;
        }

        // 单独处理奇数圈
        if( n % 2 == 1) {
            nums[startX][startY] = count;
        }

        return nums;

    }
}
